package storage

import (
	"sync"

	"gitee.com/mkwchecking/sr-core/storage/cache"
	"gitee.com/mkwchecking/sr-core/storage/database"
)

func newDefaultStorage() IStorage {
	return &storage{
		lock: sync.Mutex{},
	}
}

type storage struct {
	db    database.IDataSet
	cache cache.ICacheSet

	lock sync.Mutex
}

// CacheSet implements IStorage.
func (s *storage) CacheSet() cache.ICacheSet {
	return s.cache
}

// DataSet implements IStorage.
func (s *storage) DataSet() database.IDataSet {
	return s.db
}

// SetCache implements IStorage.
func (s *storage) SetCache(cache cache.ICacheSet) error {
	s.lock.Lock()
	defer s.lock.Unlock()

	s.cache = cache
	return nil
}

// SetDataSet implements IStorage.
func (s *storage) SetDataSet(dataSet database.IDataSet) error {
	s.lock.Lock()
	defer s.lock.Unlock()

	s.db = dataSet
	return nil
}
